<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use common\models\datas\CardAttr;
use common\models\datas\Customer;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%complaint}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property integer $mechanic_id
 * @property integer $service_level
 * @property integer $teaching_level
 * @property integer $environmental_level
 * @property integer $status
 * @property string $complaint_content
 * @property integer $admin_id
 * @property string $admin_name
 * @property string $remarks
 * @property integer $created_at
 * @property integer $updated_at
 */
class Scholarapply extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%scholarapply}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['telephone', 'student_name', 'region', 'admin_name','remarks'], 'string'],
            [['telephone'], 'match', 'pattern' => '/^1[3|4|5|7|8]\d{9}$/', 'message' => '请正确输入手机号码'],
            [['student_age', 'admin_id','created_at','updated_at','status','customer_id','cardcode_id'], 'integer'],
            [['admin_name'], 'string', 'max' => 50],
            [['telephone','student_name'],'required'],
            [['telephone'],'canotself_check'],//不可以填自身的手机号
            [['status'],'required','on' => ['handle']],
            [['remarks'],'remarks_required','skipOnEmpty'=>false,'on' => ['handle']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'customer_id' => Yii::t('zhtfrontend', '会员'),
            'telephone' => Yii::t('zhtfrontend', '申请人手机号'),
            'student_name' => Yii::t('zhtfrontend', '学员姓名'),
            'student_age' => Yii::t('zhtfrontend', '学员年龄'),
            'region' => Yii::t('zhtfrontend', '住址区域'),
            'admin_id' => Yii::t('zhtfrontend', '处理人id'),
            'admin_name' => Yii::t('zhtfrontend', '处理人员姓名'),
            'cardcode_id' => Yii::t('zhtfrontend', '激活码id'),
            'status' => Yii::t('zhtfrontend', '状态'),
            'created_at' => Yii::t('zhtfrontend', '申请时间'),
            'updated_at' => Yii::t('zhtfrontend', '处理时间'),
            'remarks' => Yii::t('zhtfrontend', '处理备注'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            if($this->updated_at >0)
                return \Yii::$app->formatter->asDatetime($this->updated_at);
            else
                return \Yii::t('zhtfrontend','--');
        };
        $fields['admin_name'] = function () {
            if($this->admin_name == null)
                return \Yii::t('zhtfrontend','--');
            else
                return $this->admin_name;

        };
        $fields['status'] = function () {
            switch($this->status){
                case 0:
                    return \Yii::t('zhtfrontend','已提交');
                case 1:
                    return \Yii::t('zhtfrontend','审核中');
                case 2:
                    return \Yii::t('zhtfrontend','申请成功');
                case 3:
                    return \Yii::t('zhtfrontend','申请失败');
            }
        };
        $fields['href'] = function () {
            return \Yii::$app->params['wechat_url'].'/scholarapply/list';
        };
        return $fields;
    }
    public function extraFields(){
        return [
            'v_code' => function(){
                return isset($this->cardAttr)?$this->cardAttr->v_code:'';
            },
            'customer_name'=>function(){
                return isset($this->customer)?$this->customer->name:'';
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                        'handle' => function ($data) {
                            if ($data['status'] > 1) {
                                return '<button class="yx-btn-disabled" type="button">处理完成</button>';
                            } else {
                                return '';
                            }
                        }
                    ]
                )->create();
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ]
        ]);
    }

    public function getCardAttr(){
        return $this->hasOne(CardAttr::className(),['id'=>'cardcode_id']);
    }

    public function getCustomer(){
        return $this->hasOne(Customer::className(),['id'=>'customer_id']);
    }

    public function remarks_required($attribute){
        if($this->status > 0 && $this->$attribute == ''){
            $this->addError($attribute, Yii::t('zhtfrontend', '处理备注不能为空'));
        }
    }

    /*申请的时候不可以填用户自己的手机号*/
    public function canotself_check($attribute){
        $customerId=\Yii::$app->user->id;
        if(!empty($customerId)){
            $customer = Customer::findOne($customerId);
            if(!empty($customer) && isset($customer->mobile) && !empty($customer->mobile)){
                if($customer->mobile == $this->$attribute){
                    $this->addError($attribute, Yii::t('zhtfrontend', '不可填写自身手机号'));
                }
            }
        }
    }

}
